// pages/jifen_order/jifen_order.js
var app = getApp()
var util = require('../../utils/util.js');
Page({
  data: {
    navindex: 0,
    According: false,
    nav: [
      { txt: '待付款' },
      { txt: '待发货' },
      { txt: '待收货' },
      { txt: '已完成' },
    ],
    // list:[],
    list: [
    //   { urls: '/images/product_banner.png', number: "E2017080356236", name: '正道货通积分商城商品正道货通积分商城商品', shuliang: '2', fuwu: '已完成', price: '200' },
    //   { urls: '/images/product_banner.png', number: "E2017080356236", name: '正道货通积分商城商品正道货通积分商城商品', shuliang: '2', fuwu: '已完成', price: '280' },
    //   { urls: '/images/product_banner.png', number: "E2017080356236", name: '正道货通积分商城商品正道货通积分商城商品', shuliang: '2', fuwu: '已完成', price: '3000' },
    //   { urls: '/images/product_banner.png', number: "E2017080356236", name: '正道货通积分商城商品正道货通积分商城商品', shuliang: '2', fuwu: '已完成', price: '240' }
    ],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    userInfo: {},
    totalPage: 1,
    currentPage: 1,
    status:0
  },
  onReachBottom: function () {
    var currentpage = this.data.currentPage;
    var totalpage = this.data.totalPage;
    if (currentpage < totalpage) {
      this.setData({
        currentPage: currentpage + 1
      })
      this.initData();
    } else {
      console.log("已经是全部数据")
      
    }
  },
  //底部
  click: function (e) {
    var id = e.currentTarget.dataset.id;
    console.log(id)
    this.setData({
      navindex: id,
      status:id
    })
 
    this.setData({
      currentPage:1,
      totalPage:1,
      list:[]
    })
   this.initData();

  },
  //提醒发货
  showok: function () {
    wx.showToast({
      title: '已成功提醒卖家发货',
      icon: 'success',
      duration: 2000
    })
  },
  //确认收货
  confirm: function (e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    var param ={id:id};
    wx.showModal({
      title: '确认收货',
      content: '确定收货成功？',
      success: function (res) {
        if (res.confirm) {
          // console.log('用户点击确定')
          util.commonRequest('confirmReceived', 'post', param, function (res) {
            // var data = res;
            if (res.data.code == 200) {
              wx.showToast({
                title: res.data.msg,
              })
              that.setData({
                currentPage: 1,
                totalPage: 1,
                list: []
              })
              that.initData();
            } else {
              wx.showToast({
                icon: 'loading',
                title: res.data.msg,
              })
            }
          });
        } else if (res.cancel) {
           
        }
      }
    })

    // wx.showToast({
    //   title: '确认收货成功',
    //   icon: 'success',
    //   duration: 2000
    // })
  },
  //删除订单
  deletes: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var del = this.data.orderdeil;
    var param = { id: id };
    wx.showModal({
      title: '删除订单',
      content: '确定要删除订单？',
      success: function (res) {
        if (res.confirm) {
          util.commonRequest('deleteOrder', 'post', param, function (res) {
            if (res.data.code == 200) {
              wx.showToast({
                title: res.data.msg,
              })
              that.setData({
                currentPage: 1,
                totalPage: 1,
                list: []
              })
              that.initData();
            } else {
              wx.showToast({
                icon: 'loading',
                title: res.data.msg,
              })
            }
          });
        } else if (res.cancel) {

        }
      }
    })
  },
  cancel: function (e) {
    var that = this;
    var id = e.currentTarget.id;
    var del = this.data.orderdeil;
    wx.showModal({
      title: '取消订单',
      content: '确定要取消订单？',
      success: function (res) {
        if (res.confirm) {
          // console.log('用户点击确定')
          for (var i in del) {
            if (i == id) {
              del.splice(id, 1);
            }
          }
          that.setData({
            orderdeil: del
          })
        } else if (res.cancel) {
          // console.log('用户点击取消')

        }
      }
    })
  },
  bindViewTap: function () {

  },
  onLoad: function (e) {
    
  },
  initData:function(){
    var that = this;
    var param ={
      status: that.data.status
    };
    util.commonRequest('pointOrder', 'post', param, function (res) {
      // var data = res;
      if (res.data.code == 200) {
        var tempArr = res.data.data.data;
        var oriArr = that.data.list;
        var finalyArr = oriArr.concat(tempArr);
        that.setData({
          list: finalyArr,
          totalPage: res.data.data.last_page
        })
      //  console.log(that.data.list)
      } else {
        wx.showToast({
          icon: 'loading',
          title: res.data.msg,
        })
      }
    });
  },
  onShow: function () {
    let that = this;
    that.initData();

    var HQgoods = that.data.orderdeil;
    if (HQgoods.length == 0) {
      that.setData({
        According: true,
      })
    } else {
      that.setData({
        According: false,
      })
    }
    
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})